package com.kuang.conroller;

import com.kuang.pojo.Candidate;
import com.kuang.pojo.Exam;
import com.kuang.pojo.Uuser;
import com.kuang.service.CandidateService;
import com.kuang.service.ExamService;
import com.kuang.service.UuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    @Qualifier("CandidateServiceImpl")
    private CandidateService CandidateService;
    @Autowired
    @Qualifier("ExamServiceImpl")
    private ExamService ExamService;

    @Autowired
    @Qualifier("CodeServiceImpl")
    private com.kuang.service.CodeService CodeService;

    @RequestMapping("/bm")
    public String addPass(HttpSession session, Exam exam, Model model){
        System.out.println(exam.getDAYS()+exam.getTIMES()+exam.getPLACE());
        Object o=session.getAttribute("USERID");
        int USERID=Integer.parseInt(o==null?"":o.toString());
        if((""+exam.getDAYS()+exam.getTIMES()+exam.getPLACE()).contains("选择")||
                exam.getDAYS()==null||exam.getTIMES()==null||exam.getPLACE()==null){
            model.addAttribute("errorMsg","请选择所有的选项");
            return "dating";
        }
        Candidate candidate= CandidateService.getCByUid(USERID);
        int cid=candidate.getCID();
        session.setAttribute("CID",cid);
        String code=""+CodeService.getNum(exam.getPLACE())+CodeService.getNum(exam.getTIMES())+CodeService.getNum(exam.getDAYS());
//        System.out.println("code=>"+code);
        Exam e1= ExamService.getExamByid(cid,code);
        String msg=null;
        if(e1==null){
            ExamService.addPass(exam.getDAYS(),exam.getTIMES(),exam.getPLACE(),code,candidate);
            e1= ExamService.getExamByid(cid,code);
            msg="报名成功了";
        }else{
            msg="考完再来报名吧！";
        }
        List<Exam> allexams=ExamService.allExam(cid);
        model.addAttribute("msg",msg);
        model.addAttribute("exam",e1);
        model.addAttribute("allexams",allexams);
        return "baoming";
    }
    @RequestMapping("/allExam")
    public String allExam(HttpSession session, Exam exam,
                          @RequestParam("USERID") int USERID, Model model){
        Candidate candidate=CandidateService.getCByUid(USERID);
        int cid=candidate.getCID();
        session.setAttribute("CID",cid);
        String msg=null;
        msg="你的考试信息如下";
        List<Exam> allexams=ExamService.allExam(cid);
        model.addAttribute("msg",msg);
        model.addAttribute("allexams",allexams);
        return "baoming1";
    }
    @RequestMapping("/login")
    public String login(HttpSession session, Exam exam, Model model){
        //用户登录
        Object o=session.getAttribute("CID");
        int cid=Integer.parseInt(o==null?"":o.toString());
        System.out.println("CID-=>"+cid);
        exam= ExamService.Login(exam.getPASSNUM(),exam.getPASSPWD(),cid);
        if(exam==null){
            List<Exam> allexams=ExamService.allExam(cid);
            model.addAttribute("allexams",allexams);
            model.addAttribute("errorMsg","准考证和密码错误");
            System.out.println("!!!!!!!!准考证和密码请错误");
            return "baoming1";
        }
        else if(exam.getSTATUS().equals("Y")){
            int EID=exam.getEID();
            session.setAttribute("EID",EID);
            return "redirect:/paper/wd";
        }
        int EID=exam.getEID();
        session.setAttribute("EID",EID);
        //加载报名考试信息
        return "redirect:/paper/start";
    }




}
